package web.controller.address_telefon;

import hibernate.pojo.User;
import hibernate.pojo_address_telefon.Address;
import hibernate.util.HibernateUtil;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import web.controller.LoginController;

public class ListAddressesController {

	
	private static final Log logger = LogFactory.getLog(LoginController.class);
	
	private List<Address> addresses;
	private Address address;
	private UIData addressesTable;
	private String name;
	private String postalCode;
	private String streetNumber;
	
	public ListAddressesController() {
		// TODO Auto-generated constructor stub
	}

	public void setTelephones(List<Address> addresses) {
		this.addresses = addresses;
	}

	public List<Address> getAddresses() {
		
		FacesContext context = FacesContext.getCurrentInstance();
		String addressBookID = (String) context.getExternalContext().getSessionMap().get("addressBookID");
		int addressBookIDForDBSelection = 0;
		try {
			addresses = new ArrayList<Address>();
			addressBookIDForDBSelection = Integer.parseInt(addressBookID);
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Address.class);
			criteria.add(Restrictions.eq("addressBookID",addressBookIDForDBSelection));
			addresses = criteria.list();
		} catch (Exception exception) {
			logger.error("Kullanici listesi database den okunurken hata olustu!",exception);
		}
		
		if(addresses != null && addresses.size() > 0){
			return addresses;
		} else 
			return null;
	}
	
	public String goruntule(){
		String pageToReturn = "";
		try {
			
			setAddress(((Address)getAddressesTable().getRowData()));
			
			setPostalCode(getAddress().getPostalCode());
			setName(getAddress().getName());
			setStreetNumber(getAddress().getStreetNumber());
			
			logger.info("Initial Address Values!");
			logger.info(getPostalCode());
			logger.info(getName());
			logger.info(getStreetNumber());
			pageToReturn = "showAddressDetailPage";
		} catch (Exception exception) {
			logger.error("Secilen adresi getirirken hata olustu!", exception);
		}
		return pageToReturn;
	}
	
	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}
	
	public String updateAddressInformation(){
		String pageToRedirect = "";
		
		logger.info("Updated Telephone Values!");
		logger.info(getPostalCode());
		logger.info(getName());
		logger.info(getStreetNumber());
		
		try {
			
//			FacesContext context = FacesContext.getCurrentInstance();
//			user = (User)context.getExternalContext().getSessionMap().get("user");
//			user.setFirstName(getFirstName());
//			user.setLastName(getLastName());
//			user.setUserName(getUserName());
			
			address.setPostalCode(getPostalCode());
			address.setName(getName());
			address.setStreetNumber(getStreetNumber());
			
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.update(address);
			transaction.commit();
		
			pageToRedirect = "updateUserInfoSuccess";
		} catch (Exception exception) {
			logger.error("Error Occured During User Info Update :", exception);
		}		
		
		return pageToRedirect;
	}

	public void setAddressesTable(UIData addressesTable) {
		this.addressesTable = addressesTable;
	}

	public UIData getAddressesTable() {
		return addressesTable;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPostalCode() {
		return postalCode;
	}

	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}

	public String getStreetNumber() {
		return streetNumber;
	}

	public void setStreetNumber(String streetNumber) {
		this.streetNumber = streetNumber;
	}


}
